package com.lee.enjoy04_221113

import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.work.*
import com.lee.enjoy04_221113.anno.AutoWired
import com.lee.enjoy04_221113.workers.MyWorker

/**
 *
 * @ProjectName: LifeCycleTest
 * @Package: com.lee.enjoy04_221113
 * @ClassName: SecondActivity
 * @Description: java类作用描述
 * @Author: 李占伟
 * @CreateDate: 2022/11/13 15:03
 * @UpdateUser: 更新者
 * @UpdateDate: 2022/11/13 15:03
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */

class SecondActivity:AppCompatActivity() {
     val TAG = SecondActivity::class.java.simpleName
    @AutoWired var  name:String = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        InjtectUtils.inject(this)
        setContentView(R.layout.activity_main_enjoy01_second)
        Log.i(TAG,"name=$name")
        findViewById<Button>(R.id.btn_jump).setOnClickListener {
            val sendData = Data.Builder().putString("lzw","liyichen")
                .build()

            val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
            val oneTimeWorkRequest = OneTimeWorkRequest.Builder(MyWorker::class.java)
                .setInputData(sendData).setConstraints(constraints)
                .build()
            WorkManager.getInstance(it.context).enqueue(oneTimeWorkRequest)


            WorkManager.getInstance(it.context).getWorkInfoByIdLiveData(oneTimeWorkRequest.id)
                .observe(this, Observer {
                    var stateName = it.state.name
                    Log.i(TAG,"stateName=$stateName")
                    if (it.state.isFinished){
                        val outdata = it.outputData
                        var string = outdata.getString("name")
                        Log.i(TAG,"收到任务返回的数据=$string")
                    }

                })
        }

    }


}